from __future__ import with_statement
import os
import os.path
from django.core.management import base

class Command(base.BaseCommand):
    can_import_settings = True
    help = "Generate liquidsoap's passwords.liq file from the site settings"

    def handle(self, *args, **kwargs):
        from django.conf import settings

        tmpl_file = os.path.join(settings.LIQ_DIR, 'passwords.liq.tmpl')

        with open(tmpl_file) as f:
            tmpl = f.read()

        params = {
            'root': settings.SITE_ROOT,
            'icecast_host': settings.ICECAST_HOST,
            'icecast_port': settings.ICECAST_PORT,
            'icecast_password': settings.ICECAST_PASSWORD,
            'icecast_mount_ogg': settings.ICECAST_MOUNT_OGG,
            'icecast_mount_mp3': settings.ICECAST_MOUNT_MP3,
            'ae2_api_key': settings.AE2_API_KEY,
            }

        actual = tmpl % params
        actual_file = os.path.join(settings.LIQ_DIR, 'passwords.liq')

        with open(actual_file, 'w') as f:
            f.write(actual)

        print 'Wrote Liquidsoap configuration to ' + actual_file
